$(document).ready(function()
{
	
	$('#commits_select').hide();
	
	//get my commits 
	$.get('/svnapproval/request/getcommits/', function(data) 
	{
		$('#commits_select').html(data);
	});
	
	
	
	$('#request_commits').click(function() 
	{

		$('#commits_select').show();
		$('#request_main').hide();
		
		// change the appareance of tabs
		$("#request_approval").attr("class", "");
		$("#request_commits").attr("class", "selected");

		

	}); 
	

	$('#request_approval').click(function() 
	{

		$('#commits_select').hide();
		$('#request_main').show();
		
		// change the appareance of tabs
		$("#request_approval").attr("class", "selected");
		$("#request_commits").attr("class", "");

		
		
		

	}); 
	
	
	$('#send_pull').click(function() 
	{
		
		 var message = $('#pull_body').val() ; 
		 var subject = $('#subject').val() ;
		 var erro = 0 ; 
		
		 var tagsArray = new Array();
	     $('input:checked').each(function()
	     {
	    	 id = $(this).attr('id') ; 
	    	 if(id!="")
	    	 { 
			   	 tagsArray.push(id);
			 }
	     }); 
	     

	     // security 
	     if(subject=="")
	     {
	    	 alert("You need fill a subject to aprover") ; 
	    	 erro ++ ; 
	    	  
	     }
	     else if (message=="")
	     {
	    	 alert("You need fill a message to aprover") ; 
	    	 erro ++ ; 
	     }
	     else if (tagsArray.length == 0)
	     {
	    	 alert("You need select the least one commit") ; 
	    	 erro ++ ; 
	     }
	     
	     
	     if(erro == 0)
	     { 
		      $.post('/svnapproval/request/submit', { 'commits[]': tagsArray , 'message': message , 'subject':subject },function(data) 
		      {
		    	  $('#result_request').html(data);
		    	  
			   		//redirect user to files home again 
			   		alert('Your request was sent to the approver!!') ; 
			   		window.location.href='/svnapproval/file/'; 

		    	  
/*		    	  
		    	    //refresh my commits   
		    		$.get('/svnapproval/request/getcommits/', function(data) 
		    		{
		    			$('#commits_select').html(data);
		    		});
	*/ 	    	  
		    	  
			  }); 
		
	     }

	      
	}); 
	
	$('#pull_comment_form').submit(function() 
	{
	
		var request  = $('#cod_request').val() ;
		var message  = $('#comment_body').val() ;
		var approver = $('#approver').val() ;
		
		
		 $.post('/svnapproval/request/submitaprove', { 'request': request , 'message': message , 'approver':approver  },function(data) 
		 {
			 
			//redirect user to files home again 
		   	alert('The request was aprroved!') ; 
		   	window.location.href='/svnapproval/request/pendent/'; 
			 
		 }) ; 
		
		
		
		//alert('Handler for .submit() called.');
		  return false;
	});
	
	$('#reprove').click(function() 
			{
			
				var request  = $('#cod_request').val() ;
				var message  = $('#comment_body').val() ;
				var approver = $('#approver').val() ;
				
				
				 $.post('/svnapproval/request/reprove', { 'request': request , 'message': message , 'approver':approver  },function(data) 
				 {
					 
					//redirect user to files home again 
				   	alert('The request was reproved!') ; 
				   	window.location.href='/svnapproval/request/pendent/'; 
					 
				 }) ; 
				
				
				
				//alert('Handler for .submit() called.');
				  return false;
			});
	
	
	
}) ; 